﻿
using Haidou.Wpf.Domian.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Application = System.Windows.Application;

namespace Haidou.Wpf.UI.Util
{
	internal class NotificationManager
	{
		public static List<Notification> Notifications { get; set; } = new();


		internal static void AddNotification(string text, StatusType kind, string tag, ICommand command = null, object commandParameter = null)
		{
			AddNotification(text, kind, tag, () => { command?.Execute(commandParameter); });
		}

		internal static void AddNotification(string text, StatusType kind, string tag, Action action = null)
		{
			Random rand = new(Notifications.Count);
			int id = rand.Next();

			while (Notifications.Any(a => a.Id == id))
			{
				id = rand.Next();
			}

			Notifications.Add(new Notification { Id = id, Text = text, Kind = kind, Tag = tag, Action = action });

			Refresh();
		}


		internal static void RemoveNotification(int id)
		{
			Notifications.RemoveAll(a => a.Id == id);

			Refresh();
		}

		internal static void RemoveNotification(Predicate<Notification> match)
		{
			Notifications.RemoveAll(match);

			Refresh();
		}

		internal static void RemoveAllNotifications()
		{
			Notifications.Clear();

			Refresh();
		}

		/// <summary>
		/// 警告所有实现通知界面的窗口通知数据已更新。
		/// </summary>
		internal static void Refresh()
		{
			foreach (INotification notification in Application.Current.Windows.OfType<INotification>())
			{
				notification.NotificationUpdated();
			}
		}
	}

	internal interface INotification
	{
		void NotificationUpdated();
	}

	internal class Notification
	{
		public int Id { get; set; }

		public string Text { get; set; }

		public StatusType Kind { get; set; }

		public string Tag { get; set; }

		public UIElement Image { get; set; }

		public Action Action { get; set; }
	}
}
